<template>
  <div class="app-container">
    <!-- 搜索和操作工具栏 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="list"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="标题" prop="title" />
      <el-table-column label="类型" prop="type">
        <template #default="{ row }">
          {{ getTypeName(row.type) }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" />
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column label="状态" prop="status">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusName(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button type="text" @click="handleView(row)">查看</el-button>
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="listQuery.page"
      v-model:limit="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listDocument, deleteDocument } from '@/api/workflow/document'
import Pagination from '@/components/Pagination'

export default {
  name: 'DocumentList',
  components: { Pagination },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        keyword: undefined
      },
      typeOptions: [
        { label: '通知公告', value: 1 },
        { label: '规章制度', value: 2 },
        { label: '会议纪要', value: 3 },
        { label: '其他', value: 4 }
      ],
      statusOptions: [
        { label: '草稿', value: 0 },
        { label: '待审批', value: 1 },
        { label: '已通过', value: 2 },
        { label: '已拒绝', value: 3 }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listDocument(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push('/document/create')
    },
    handleView(row) {
      this.$router.push(`/document/view/${row.id}`)
    },
    handleEdit(row) {
      this.$router.push(`/document/edit/${row.id}`)
    },
    handleDelete(row) {
      this.$confirm('确认删除该记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteDocument(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getTypeName(type) {
      const found = this.typeOptions.find(item => item.value === type)
      return found ? found.label : ''
    },
    getStatusName(status) {
      const found = this.statusOptions.find(item => item.value === status)
      return found ? found.label : ''
    },
    getStatusType(status) {
      const map = {
        0: 'info',
        1: 'warning',
        2: 'success',
        3: 'danger'
      }
      return map[status]
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  padding-bottom: 10px;
  .filter-item {
    margin-right: 10px;
  }
}
</style> 